/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfigurationUI.java
 *
 * Created on May 27, 2009, 3:07:14 PM
 */
package schooltimetablecsp;

import java.util.Iterator;
import javax.swing.JOptionPane;

/**
 *
 * Configuration user interface
 */
public class CombineUI extends javax.swing.JFrame {

    protected int n_teacher;
    protected int n_class;
    protected int n_subject;

    /** Creates new form ConfigurationUI */
    public CombineUI() {
        initComponents();
        fillComponents();
    }

    public void fillComponents()
    {
        this.n_teacher = TeacherUI.all_teacher.size();
        Iterator<Teacher> tt_it = TeacherUI.all_teacher.iterator();
        while(tt_it.hasNext())
        {
            Teacher tt = (Teacher) tt_it.next();
            TeacherCombo.addItem(tt);
        }
        this.teacherTextField.setText(Integer.toString(n_teacher));

        this.n_class = ClassUI.all_class.size();
        Iterator<LeisureClass> lc_it = ClassUI.all_class.iterator();
        while(lc_it.hasNext())
        {
            LeisureClass lc = (LeisureClass) lc_it.next();
            ClasseCombo.addItem(lc);
        }
        this.classTextField.setText(Integer.toString(n_class));

        this.n_subject = SubjectUI.all_subj.size();
        Iterator<Subject> ss_it = SubjectUI.all_subj.iterator();
        while(ss_it.hasNext())
        {
            Subject ss = (Subject) ss_it.next();
            SubjectCombo.addItem(ss);
        }
        this.subjectTextField.setText(Integer.toString(n_subject));
        ClasseCombo.setSelectedIndex(0);
        TeacherCombo.setSelectedIndex(0);
        SubjectCombo.setSelectedIndex(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        ClasseCombo = new javax.swing.JComboBox();
        SubjectCombo = new javax.swing.JComboBox();
        TeacherCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        classTextField = new javax.swing.JTextField();
        subjectTextField = new javax.swing.JTextField();
        teacherTextField = new javax.swing.JTextField();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("null");

        jButton1.setText("Exit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("Add");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("CLASS");

        jLabel2.setText("SUBJECT");

        jLabel3.setText("TEACHER");

        classTextField.setColumns(3);
        classTextField.setEditable(false);
        classTextField.setDragEnabled(false);
        classTextField.setOpaque(true);

        subjectTextField.setColumns(3);
        subjectTextField.setEditable(false);
        subjectTextField.setDragEnabled(false);
        subjectTextField.setOpaque(true);

        teacherTextField.setColumns(3);
        teacherTextField.setEditable(false);
        teacherTextField.setDragEnabled(false);
        teacherTextField.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ClasseCombo, 0, 140, Short.MAX_VALUE)
                    .addComponent(SubjectCombo, 0, 140, Short.MAX_VALUE)
                    .addComponent(TeacherCombo, 0, 140, Short.MAX_VALUE))
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(classTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(teacherTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(subjectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(83, 83, 83)
                .addComponent(jButton3)
                .addGap(61, 61, 61)
                .addComponent(jButton1)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ClasseCombo)
                    .addComponent(jLabel1)
                    .addComponent(classTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SubjectCombo)
                    .addComponent(jLabel2)
                    .addComponent(subjectTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TeacherCombo)
                    .addComponent(jLabel3)
                    .addComponent(teacherTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Teacher tt = (Teacher)TeacherCombo.getSelectedItem();
        Subject ss = (Subject)SubjectCombo.getSelectedItem();
        LeisureClass lc = (LeisureClass)ClasseCombo.getSelectedItem();

        int tot_hours = lc.to_assign.size();
        int ass_hours = 0;
        int pro_hours = 0;
        Iterator it_ss = tt.modules.iterator();
        while(it_ss.hasNext())
        {
            Subject temp_s = (Subject)it_ss.next();
            ass_hours += temp_s.n_slot;
        }
        Iterator it_tt = tt.modules.iterator();
        while(it_tt.hasNext())
        {
            Subject temp_s = (Subject)it_tt.next();
            pro_hours += temp_s.n_slot;
        }

        if (tot_hours == ass_hours)
        {
            JOptionPane.showMessageDialog(null, "Class already full!", "Error", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            if (tot_hours < (ass_hours + ss.n_slot))
            {
                JOptionPane.showMessageDialog(null, "There are too many hours to assign in this class!", "Error", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                if (tot_hours >= (ass_hours + ss.n_slot))
                {
                    SubjectCombo.removeItem(ss);
                    //tt.modules.add(ss);
                    ss.classe = lc;
                    ss.insegnante = tt;
                    //lc.modules.add(ss);
                    this.n_subject --;
                    if (tot_hours == (ass_hours + ss.n_slot))
                    {
                        ClasseCombo.removeItem(lc);
                        this.n_class --;
                    }
                    System.out.println(pro_hours + " " + ss.n_slot + " vs " + tot_hours);
                    if (tot_hours == (pro_hours + ss.n_slot))
                    {
                        TeacherCombo.removeItem(tt);
                        this.n_teacher --;
                    }
                }
            }
        }

        subjectTextField.setText(Integer.toString(n_subject));
        teacherTextField.setText(Integer.toString(n_teacher));
        classTextField.setText(Integer.toString(n_class));

        if (n_subject == 0 || n_teacher == 0 || n_class == 0)
            this.jButton3.setEnabled(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox ClasseCombo;
    private javax.swing.JComboBox SubjectCombo;
    private javax.swing.JComboBox TeacherCombo;
    private javax.swing.JTextField classTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField subjectTextField;
    private javax.swing.JTextField teacherTextField;
    // End of variables declaration//GEN-END:variables
}
